import urllib.parse
import urllib.request

def create_request(page):
    url = 'http://www.kfc.com.cn/kfccda/ashx/GetStoreList.ashx?op=cname'
    data = {
        'cname': '北京',
        'pid':'',
        'pageIndex': page,
        'pageSize': 10
    }
    headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36 Edg/120.0.0.0'}
    new_data = urllib.parse.urlencode(data).encode('utf-8')
    request = urllib.request.Request(url=url,data=new_data,headers=headers)
    return request
def create_com(request):
    opens = urllib.request.urlopen(request)
    com = opens.read().decode('utf-8')
    return com

def create_file(com,page):
    df = open('../爬取的文件/kdj_'+str(page)+'.json','w',encoding='utf-8')
    df.write(com)



if __name__ == '__main__':
    start_page = int(input('请输入起始页码：'))
    end_page = int(input('请输入结束页码：'))

    for page in range(start_page,end_page+1):
        print(page)
        request = create_request(page)
        com = create_com(request)
        create_file(com,page)
